home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / kernel / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  3.6 KB  |  205 lines  |  [TEXT/R*ch]

  1. /* MSDOS-specific code for Xconq.
  2.    Copyright (C) 1994 Ed Boston.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "config.h"
  10. #include "misc.h"
  11. #include "dir.h"
  12. #include "lisp.h"
  13. #include "module.h"
  14. #include "system.h"
  15.  
  16. #include <signal.h>
  17. #include <time.h>
  18. #include <sys/types.h>
  19.  
  20. typedef void (*fptr)();
  21.  
  22. #ifndef XCONQLIB
  23. #define XCONQLIB "..\\lib"
  24. #endif
  25.  
  26. char *
  27. default_library_filename()
  28. {
  29.     return XCONQLIB;
  30. }
  31.  
  32. char *
  33. news_filename()
  34. {
  35.     make_pathname(xconqlib, NEWSFILE, "", spbuf);
  36.     return spbuf;
  37. }
  38.  
  39. char *
  40. saved_game_filename()
  41. {
  42.     return "save.xcq";
  43. }
  44.  
  45. char *
  46. checkpoint_filename()
  47. {
  48.     return "checkpnt.xcq";
  49. }
  50.  
  51. char *
  52. error_save_filename()
  53. {
  54.     return "errorsav.xcq";
  55. }
  56.  
  57. char *
  58. statistics_filename()
  59. {
  60.     return "stats.xcq";
  61. }
  62.  
  63. /* Attempt to open a library file. */
  64.  
  65. FILE *
  66. open_library_file(module)
  67. Module *module;
  68. {
  69.     FILE *fp = NULL;
  70.  
  71.     /* Don't try to do on anon modules? */
  72.     if (module->name == NULL) return NULL;
  73.     /* Generate library pathname. */
  74.     make_pathname(xconqlib, module->name, "g", spbuf);
  75.     /* Now try to open the file. */
  76.     if ((fp = fopen(spbuf, "r")) != NULL) {
  77.         /* Remember the filename where we found it. */
  78.         module->filename = copy_string(spbuf);
  79.     } else {
  80.         /* Extra hack to find experimental games. */
  81.         make_pathname("../lib2", module->name, "g", spbuf);
  82.         if ((fp = fopen(spbuf, "r")) != NULL) {
  83.             /* Remember the filename where we found it. */
  84.             module->filename = copy_string(spbuf);
  85.         }
  86.     }
  87.     return fp;
  88. }
  89.  
  90. FILE *
  91. open_explicit_file(module)
  92. Module *module;
  93. {
  94.      if (module->filename == NULL) return NULL;
  95.      return (fopen(module->filename, "r"));
  96. }
  97.  
  98. FILE *
  99. open_library_file(filename)
  100. char *filename;
  101. {
  102.     FILE *fp = NULL;
  103.  
  104.     /* Don't try to do on anon modules? */
  105.     /* Generate library pathname. */
  106.     make_pathname(xconqlib, filename, NULL, spbuf);
  107.     /* Now try to open the file. */
  108.     if ((fp = fopen(spbuf, "r")) != NULL) {
  109.     } else {
  110.     }
  111.     return fp;
  112. }
  113.  
  114. void
  115. make_pathname(path, name, extn, pathbuf)
  116. char *path, *name, *extn, *pathbuf;
  117. {
  118.     sprintf(pathbuf, "");
  119.     if (!empty_string(path)) {
  120.         sprintf(pathbuf+strlen(pathbuf), "%s/", path);
  121.     }
  122.     sprintf(pathbuf+strlen(pathbuf), "%s", name);
  123.     /* Don't add a second identical extension, but do add if extension
  124.         is different (in case we want "foo.12" -> "foo.12.g" for instance) */
  125.     if (strrchr(name, '.') && strcmp((char *) strrchr(name, '.')+1, extn) == 0)
  126.         return;
  127.     if (!empty_string(extn)) {
  128.         sprintf(pathbuf+strlen(pathbuf), ".%s", extn);
  129.     }
  130. }
  131.  
  132. /* Remove a saved game from the system. */
  133.  
  134. remove_saved_game()
  135. {
  136.     unlink(saved_game_filename());
  137. }
  138.  
  139. /* Default behavior on explicit kill. */
  140.  
  141. void
  142. stop_handler(x)
  143. int x;
  144. {
  145.     close_displays();
  146.     exit(1);
  147. }
  148.  
  149. /* This routine attempts to save the state before dying. */
  150.  
  151. void
  152. crash_handler(sig)
  153. int sig;
  154. {
  155.     static int already_been_here = FALSE;
  156.  
  157.     if (!already_been_here) {
  158.         already_been_here = TRUE;
  159.         close_displays();
  160.         printf("Fatal error encountered. Signal %d\n", sig);
  161.         write_entire_game_state("xconq.ack");
  162.     }
  163.     abort();
  164. }
  165.  
  166. void
  167. init_signal_handlers(void)
  168. {
  169.     signal(SIGINT, stop_handler);
  170.     signal(SIGSEGV, crash_handler);
  171.     signal(SIGFPE, crash_handler);
  172.     signal(SIGILL, crash_handler);
  173.     signal(SIGINT, crash_handler);
  174.     signal(SIGTERM, crash_handler);
  175. }
  176.  
  177.  
  178. time_t reallasttime = (time_t) 0;
  179. time_t realcurtime;
  180.  
  181. int
  182. n_seconds_elapsed(n)
  183. int n;
  184. {
  185.     time(&realcurtime);
  186.     if (realcurtime > reallasttime + (n - 1)) {
  187.         reallasttime = realcurtime;
  188.         return TRUE;
  189.     } else {
  190.         return FALSE;
  191.     }
  192. }
  193.  
  194. int
  195. n_ms_elapsed(n)
  196. int n;
  197. {
  198.     return TRUE;
  199. }
  200.  
  201. void
  202. record_ms()
  203. {
  204. }
  205.